//
//  main.m
//  08_Work
//
//  Created by dllo on 16/7/12.
//  Copyright © 2016年 yh. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Province.h"
#import "City.h"
#import "District.h"
#import "TempCity.h"

int main(int argc, const char * argv[]) {
    // 1.使用MRC, 重写用类处理的两个省市区。加入对应内存管理。
    // 2.复习Objective-C语言的基本特性
#if 1
    NSString *areaStr = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/08_Work/08_Work/area.txt" encoding:NSUTF8StringEncoding error:nil];
    NSArray *areaArr = [areaStr componentsSeparatedByString:@"\n"];
    NSMutableArray *bigArr = [NSMutableArray array];
    for (NSString *area in areaArr) {
        if (![area hasPrefix:@" "]) {
            Province *province = [[Province alloc] initWithName:area];
            [bigArr addObject:province];
        } else if (![area hasPrefix:@"    "]) {
            City *city = [[City alloc] initWithName:area];
            [[[bigArr lastObject] arr] addObject:city];
        } else {
            District *district = [[District alloc] initWithName:area];
            [[[[[bigArr lastObject] arr] lastObject] arr] addObject:district];
        }
    }
    NSLog(@"%@", bigArr);
#endif
#if 0
    NSString *locationStr = [NSString stringWithContentsOfFile:@"/Users/dllo/Desktop/08_Work/08_Work/Location1.txt" encoding:NSUTF8StringEncoding error:nil];
        NSArray *allLocationArr = [locationStr componentsSeparatedByString:@","];
    NSMutableArray *bigArray = [NSMutableArray array];
    for (NSString *str in allLocationArr) {
        NSArray *arr = [str componentsSeparatedByString:@"|"];
        TempCity *tempCity = [[TempCity alloc] initWithName:arr.firstObject number:arr.lastObject];
        [bigArray addObject:tempCity];
    }
    NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"number" ascending:YES];
    [bigArray sortUsingDescriptors:@[sort]];
    NSMutableArray *allPlaceArr = [NSMutableArray array];
    for (TempCity *tempCity in bigArray) {
        if ([tempCity.number hasSuffix:@"0000"]) {
            Province *pro = [Province districtWithTempCity:tempCity];
            [allPlaceArr addObject:pro];
        } else if ([tempCity.number hasSuffix:@"00"]) {
            City *city = [City districtWithTempCity:tempCity];
            Province *pro = [allPlaceArr lastObject];
            [pro.arr addObject:city];
        } else {
            District *dis = [District districtWithTempCity:tempCity];
            Province *pro = allPlaceArr.lastObject;
            City *city = pro.arr.lastObject;
            [city.arr addObject:dis];
        }
    }
    NSLog(@"%@", allPlaceArr);
#endif
    
    
    
    
    return 0;
}
